package com.citcc.web.controller.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.io.IOUtils;

import com.aliyuncs.utils.SslUtils;

public class SslTest {

    public static String getRequest(String url) throws Exception{
        URL u = new URL(url);
        if("https".equalsIgnoreCase(u.getProtocol())){
            SslUtils.ignoreSsl();
        }
        URLConnection conn = u.openConnection();
        conn.setConnectTimeout(60000);
        conn.setReadTimeout(60000);
        return IOUtils.toString(conn.getInputStream());
    }

    public static String postRequest(String urlAddress,String args,int timeOut) throws Exception{
        URL url = new URL(urlAddress);
        if("https".equalsIgnoreCase(url.getProtocol())){
            SslUtils.ignoreSsl();
        }
        URLConnection u = url.openConnection();
        u.setDoInput(true);
        u.setDoOutput(true);
        u.setConnectTimeout(timeOut);
        u.setReadTimeout(timeOut);
        OutputStreamWriter osw = new OutputStreamWriter(u.getOutputStream(), "UTF-8");
        osw.write(args);
        osw.flush();
        osw.close();
        u.getOutputStream();
        return IOUtils.toString(u.getInputStream());
    }

    public static void main(String[] args) {
        int count=90;
        for (int i = 0; i < count; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        SslTest st = new SslTest();
                       // String a = st.getRequest("http://apicloud.mob.com/oil/price/province/query?key=293f2d146c46a", 3000);
                       // System.out.println(a);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

      /* try {
            for (int i = 0; i < 10000; i++) {
                SslTest st = new SslTest();
                String a = st.getRequest("http://apicloud.mob.com/oil/price/province/query?key=293f2d146c46a", 3000);
                System.out.println(a);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }*/
    }
    public static String postBody(String urlPath, String json, String tk) throws Exception {
        URL url = new URL(urlPath);
        if("https".equalsIgnoreCase(url.getProtocol())){
            SslUtils.ignoreSsl();
        }
        String body = "";
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
        urlConnection.setRequestProperty("Authorization", tk);
        try( OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());) {
            out.write(json);
            out.flush();
            InputStream inputStream = urlConnection.getInputStream();
            String encoding = urlConnection.getContentEncoding();
            body = IOUtils.toString(inputStream, encoding);
            urlConnection.getResponseMessage();
            System.out.println("responseMessage===="+ urlConnection.getResponseMessage());
            System.out.println( "responsecode===="+ urlConnection.getResponseCode());
            System.out.println("body======="+body);
            if (urlConnection.getResponseCode() != 200 && urlConnection.getResponseCode() != 201) {
                throw new IOException() ;
            }
            return body;
        } catch (IOException e) {
            throw  e;
        }
    }
    public static String doPost(String urlPath, String json) throws Exception {
        URL url = new URL(urlPath);
        if("https".equalsIgnoreCase(url.getProtocol())){
            SslUtils.ignoreSsl();
        }
        String body = "";
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setRequestProperty("content-type", "application/x-www-form-urlencoded");
        try( OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());) {
            out.write(json);
            out.flush();
            InputStream inputStream = urlConnection.getInputStream();
            String encoding = urlConnection.getContentEncoding();
            body = IOUtils.toString(inputStream, encoding);
            urlConnection.getResponseMessage();
            System.out.println("responseMessage===="+ urlConnection.getResponseMessage());
            System.out.println( "responsecode===="+ urlConnection.getResponseCode());
            System.out.println("body======="+body);
            if (urlConnection.getResponseCode() != 200 && urlConnection.getResponseCode() != 201) {
                throw new IOException() ;
            }
            return body;
        } catch (IOException e) {
            throw  e;
        }
    }
    public static String postBodyWithTK(String urlPath, String json, String tk) throws Exception {
        URL url = new URL(urlPath);
        if("https".equalsIgnoreCase(url.getProtocol())){
            SslUtils.ignoreSsl();
        }
        String body = "";
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setRequestProperty("content-type", "application/json");
        urlConnection.setRequestProperty("Authorization", tk);
        try( OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());) {
            out.write(json);
            out.flush();
            InputStream inputStream = urlConnection.getInputStream();
            String encoding = urlConnection.getContentEncoding();
            body = IOUtils.toString(inputStream, encoding);
            urlConnection.getResponseMessage();
            System.out.println( urlConnection.getResponseMessage());
            System.out.println( urlConnection.getResponseCode());
            System.out.println("body"+body);
            if (urlConnection.getResponseCode() != 200 && urlConnection.getResponseCode() != 201) {
                throw new IOException() ;
            }
            return body;
        } catch (IOException e) {
            throw new IOException() ;
        }
    }
    public static String postJson(String urlPath, String json) throws Exception {
        URL url = new URL(urlPath);
        if("https".equalsIgnoreCase(url.getProtocol())){
            SslUtils.ignoreSsl();
        }
        String body = "";
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setDoOutput(true);
        urlConnection.setRequestProperty("content-type", "application/json");
        try( OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());) {
            out.write(json);
            out.flush();
            InputStream inputStream = urlConnection.getInputStream();
            String encoding = urlConnection.getContentEncoding();
            body = IOUtils.toString(inputStream, encoding);
            urlConnection.getResponseMessage();
            System.out.println( urlConnection.getResponseMessage());
            System.out.println( urlConnection.getResponseCode());
            System.out.println("body"+body);
            if (urlConnection.getResponseCode() != 200 && urlConnection.getResponseCode() != 201) {
                throw new IOException() ;
            }
            return body;
        } catch (IOException e) {
            throw new IOException() ;
        }
    }

}

